package edu.stanford.bmir.protege.web.server.stkosutil.merger;

import java.util.Comparator;

import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.MatchLevel;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.UtilConstants;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.merger.MatchMode;

public class MergedClassHolderComparator implements Comparator<MergedClassHolder> {

	private MatchMode matchMode = MatchMode.MAPPING_RESULT;

	public MergedClassHolderComparator(MatchMode matchMode){
		this.matchMode=matchMode;
	}

	public int compare(MergedClassHolder o1, MergedClassHolder o2) {
		String name1 = o1.getBrowserText();
		String name2 = o2.getBrowserText();
		int mappingNo1 = o1.getMappingNo();
		int mappingNo2 = o2.getMappingNo();
		name1 = (name1==null)?"":name1;
		name2 = (name2==null)?"":name2;
		if (this.matchMode==MatchMode.MAPPING_RESULT){
			return o1.getMappingNo() - o2.getMappingNo();
		}else if (this.matchMode==MatchMode.CASE_INSENSITIVE){
			return name1.compareToIgnoreCase(name2);
		}else if (this.matchMode==MatchMode.INCLUSIVE){
			name1 = name1.toLowerCase();
			name2 = name2.toLowerCase();
			if (name1.indexOf(name2)!=-1 || name2.indexOf(name1)!=-1){
				return 0;
			}else{
				return name1.compareToIgnoreCase(name2);
			}
		}else{
			return name1.compareToIgnoreCase(name2);
		}
	}

}
